package com.ruibo.modules.system.service.impl;

import com.ruibo.utils.PageUtil;
import com.ruibo.modules.system.domain.SysPost;
import com.ruibo.modules.system.repository.SysPostRepository;
import com.ruibo.modules.system.service.dto.SysPostDto;
import com.ruibo.modules.system.service.mapper.SysPostMapper;
import com.ruibo.modules.system.service.SysPostService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageRequest;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (SysPost)表服务实现类
 *
 * @author sunfeng
 * @since 2020-04-17 17:40:32
 */
@Service("sysPostService")
public class SysPostServiceImpl implements SysPostService {
    @Resource
    private SysPostRepository sysPostRepository;

    @Resource
    private SysPostMapper sysPostMapper;


    @Override
    public SysPostDto queryById(Long postId) {
        return this.sysPostMapper.toDto(this.sysPostRepository.getOne(postId));
    }

    @Override
    public List<SysPostDto> findAll() {

        return this.sysPostMapper.toDto(this.sysPostRepository.findAll());

    }

    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        List<Long> collect = sysPostRepository.findBySysUsers_Id(userId).stream()
                .map(SysPost::getId)
                .collect(Collectors.toList());
        return collect;
    }

    @Override
    public Object queryAllByLimit(int offset, int limit) {

      return PageUtil.toPage(this.sysPostRepository.findAll(PageRequest.of((offset - 1)
                * limit, limit)).map(sysPostMapper::toDto));

    }

    @Override
    public SysPostDto insert(SysPost sysPost) {

            return this.sysPostMapper.toDto(
            this.sysPostRepository.save(sysPost));


    }


    @Override
    public SysPostDto update(SysPost sysPost) {

     return this.sysPostMapper.toDto(
            this.sysPostRepository.save(sysPost));
    }


    @Override
    public boolean deleteById(Long postId) {

     try{
             this.sysPostRepository.deleteById(postId) ;
        }catch (Exception ex){
            return false;
        }
        return true;

    }
}
